/*!
** @file overlay.js
** @brief Manages the overlay with Firefox. Handles its opening and it shutting.
** @author Adrien MOGENET
** @since 0.1rc
*/

/*!
** @brief Add an item in Firefox's menu
** @since 0.1rc
** @author Adrien MOGENET
*/
function addMenu ()
{
    var menu	= document.getElementById ("bookmarksMenu");
    var popup	= menu.firstChild;
    var sep	= popup.getElementsByTagName ("menuseparator")[0];
    var element = document.createElementNS ("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "menuitem");
    var bundle	= document.getElementById ("strings");
    var label	= bundle.getString ("uploadnow");

    element.setAttribute ("id", "bookmarks-menuitem-upload");
    element.setAttribute ("label", label);
    element.setAttribute ("accesskey", "S");
    element.setAttribute ("oncommand", "keepmybookmarks();");
    popup.insertBefore (element, sep);
    BrowserToolboxCustomizeDone (true);
}

/*!
** @brief Delete the menu item that the extension added
** @since 0.1rc
** @author Adrien MOGENET
** @todo fixme
*/
function deleteMenu ()
{
    try
    {
	var item = document.getElementById ("bookmarks-menuitem-upload");
    }
    catch (error) { }
}

/*!
** @brief When the browser has been opened
** A new item is insert into the current "bookmarks" menu
** @since 0.1rc
** @author Adrien MOGENET
*/
function browserOpened ()
{
    addMenu ();
}

/*!
** @brief When the browser is closed.
** The bookmarks are uploaded if the option 'auto-upload' is enabled.
** @see function @c keepmybookmarks()
** @author Adrien MOGENET
*/
function browserClosed ()
{
    deleteMenu ();
    if (prefAutoUploadGet ())
	keepmybookmarks ()
}

/*!
** When Firefox is unloaded, we launch the main function of this
** extension
*/
window.addEventListener("load", browserOpened, false);
window.addEventListener("unload", browserClosed, false);
